package com.example.flowerpcback.services;

import com.example.flowerpcback.dto.Menus;
import com.example.flowerpcback.dto.MenusExample;
import com.example.flowerpcback.mapper.MenusMapper;
import com.example.flowerpcback.mapper.MenusMyMapper;
import com.example.flowerpcback.req.MenusReq;
import com.example.flowerpcback.req.UserReq;
import com.example.flowerpcback.resp.MenusResp;
import com.example.flowerpcback.resp.UserResp;
import com.example.flowerpcback.utils.CopyUtil;
import com.example.flowerpcback.utils.MenuUtil;
import com.example.flowerpcback.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MenusService {
    private static final Logger LOG = LoggerFactory.getLogger(MenusService.class);

    @Resource
    private MenusMapper menusMapper;

    @Resource
    private MenusMyMapper menusMyMapper;

    @Resource
    private SnowFlake snowFlake;

    public List<MenusResp> all(MenusReq req) {
        MenusExample menusExample = new MenusExample();
        menusExample.setOrderByClause("sort desc");
        MenusExample.Criteria criteria = menusExample.createCriteria();
        if(req.getName() != null) {
            criteria.andNameEqualTo(req.getName());
        }
        if(req.getStatus() != null) {
            criteria.andStatusEqualTo(req.getStatus());
        }

        List<Menus> menus = menusMapper.selectByExample(menusExample);

        List<MenusResp> menusResp = new CopyUtil().copyList(menus,MenusResp.class);
        List<MenusResp> res = new ArrayList<>();
        for (MenusResp menu : menusResp){
            if(menu.getParentid() == null) {

                menu.setChildren(new MenuUtil().findChilds(menu.getId(),menusResp));
                res.add(menu);
            }
        }

        return res;
    }

//    public PageResp<UserResp> myList(UserReq req) {
//        PageHelper.startPage(req.getPage(),req.getSize());
//
//        List<UserResp> userList = userMyMapper.selectUser(req.getRoleid(),req.getNickname(),req.getCreateTime(),req.getUpdateTime());
//
//        PageInfo<UserResp> pageInfo = new PageInfo<>(userList);
//        PageResp<UserResp> pageResp = new PageResp<>();
//        pageResp.setTotal(pageInfo.getTotal());
//        pageResp.setPages(pageInfo.getPages());
//
//        pageResp.setList(userList);
//
//        return pageResp;
//    }

    public MenusResp save(MenusReq req){


        if(req.getId() == null){
            Long id = snowFlake.nextId();
            req.setId(id);
            req.setCreateTime(new Date());
            req.setUpdateTime(new Date());

            MenusExample menusExample = new MenusExample();

            MenusExample.Criteria criteria = menusExample.createCriteria();

            if(req.getParentid() != null) {
                criteria.andParentidEqualTo(req.getParentid());
            }else{
                criteria.andParentidIsNull();
            }

            List<Menus> menuDatas = menusMapper.selectByExample(menusExample);
            if(menuDatas.size() > 0) {
                Menus menusLastData = menuDatas.get(menuDatas.size()-1);
                req.setSort(menusLastData.getSort()+1);
            }else{
                req.setSort(1);
            }

            Menus menus = new CopyUtil().copy(req, Menus.class);

            menusMapper.insert(menus);
        }else {
            req.setUpdateTime(new Date());

            Menus menus = new CopyUtil().copy(req, Menus.class);

            menusMapper.updateByPrimaryKeySelective(menus);
        }

        MenusResp menusResp =  new CopyUtil().copy(req, MenusResp.class);
        return menusResp;

    }

    public List<Menus> batchUpdate(List<MenusReq> req){

        List<Menus> menus = new CopyUtil().copyList(req, Menus.class);
        menusMyMapper.batchUpdate(menus);

        return menus;
    }

    public void delete(MenusReq req){
        menusMapper.deleteByPrimaryKey(req.getId());

        MenusExample menusExample = new MenusExample();

        MenusExample.Criteria criteria = menusExample.createCriteria();

        criteria.andParentidEqualTo(req.getId());

        menusMapper.deleteByExample(menusExample);
    }
}
